<?php


namespace app\admin\controller\pointgoods;

use app\common\controller\Adminbase;
use think\Db;

class Goodse extends Adminbase
{
    protected $searchFields = 'name';
    protected $goods_categoty;

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        //商品类型 商品名称 所需积分 排序 库存 兑换数量  操作：删除 编辑 种类不更改
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['is_del', '=', 0];
            $where[] = ['goods_area', '=', 5];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['goods_cate.name'])) {
                    $where[] = ['cate_id', '=', $filter['goods_cate.name']];
                }
                if (isset($filter['status'])) {
                    $where[] = ['status', '=', $filter['status']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['name'])) {
                    $where[] = ['name', 'like', '%' . $filter['name'] . '%'];
                }
            }
            $_list = Db::name('goods')
                ->order('sort desc')
                ->where($where)
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['goods_cate'] = Db::name('goods_cate')->find($v['cate_id']);
                $_list[$k]['brand'] = Db::name('brand')->find($v['brand_id']);
                $goods_attr_value = Db::name('goods_attr_value')
                    ->where('goods_id', '=', $v['id'])
                    ->order('id')
                    ->find();
                $sale_num = Db::name('goods_attr_value')
                    ->where('goods_id', '=', $v['id'])->sum('sale_num');
                $stock = Db::name('goods_attr_value')
                    ->where('goods_id', '=', $v['id'])->sum('stock');
                $spec_num = Db::name('goods_attr_value')
                    ->where('goods_id', '=', $v['id'])->count();
                $_list[$k]['price'] = $goods_attr_value['price'];
           //     $_list[$k]['sale_num'] = $sale_num;
                $_list[$k]['spec_num'] = $spec_num;
                $_list[$k]['stock'] = $stock;
            }
            $total = Db::name('goods')
                ->where($where)
                ->where('is_del', '<>', 1)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        $goods_categoty = Db::name('goods_cate')
            ->where('is_del', '<>', 1)
            ->order('sort desc')
            ->select();
        foreach ($goods_categoty as $k => $v) {
            $goods_categoty2[$v['id']] = $v['name'];
        }
        $this->assign('goodsCategoty', $goods_categoty2);
        return $this->fetch();
    }

    public function hdedit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            foreach ($data['suk'] as $k => $v) {
                $stmt = Db::name('goods_attr_value')->where('id',$k)->update([
                    'coupons'=>$v['coupons'],
                    'r_coupons'=>$v['r_coupons'],
                ]);
            }
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('goods_attr_value')->where([
            'is_del' => 0,
            'goods_id' => $id
        ])->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['is_spec'] == 0;
            $goods_insert_data = array(
                'goods_area' => 5,
                'cate_id' => $data['cate_id'],
                'brand_id' => $data['brand_id'],
                'name' => $data['name'],
                'points' => $data['points'],
                'unit' => $data['unit'],
                'desc' => $data['desc'],
                'hz_no' => $data['hz_no'],
                'zc_no' => $data['zc_no'],
                'shop_return' => $data['shop_return'],
                'delivery_type' => $data['delivery_type'],
                'img' => $data['img'],
                'is_spec' => $data['is_spec'],
                'sort' => $data['sort'],
                'effect' => $data['effect'],
                'quality' => $data['quality'],
                'product_address' => $data['product_address'],
                'detail' => $data['detail'],
                'guarantee' => $data['guarantee'],
                'send_address' => $data['send_address'],
                'create_time' => time(),
                'supply_id' => $data['supply_id'],
            );
            $first_img = explode(',', $data['img']);
            $goods_insert_data['first_img'] = $first_img[0];
            $goods_id = Db::name('goods')->insertGetId($goods_insert_data);

            if ($data['is_spec'] == 1) {
                $goods_attr_insert_data = array(
                    'goods_id' => $goods_id,
                    'attr_name' => $data['attr_name'],
                    'attr_value' => $data['attr_value'],
                );
                $goods_attr_id = Db::name('goods_attr')
                    ->insertGetId($goods_attr_insert_data);
                foreach ($data['spec'] as $k => $v) {
                    $goods_attr_value_insert_data = array(
                        'goods_id' => $goods_id,
                        'suk' => $v['spec_name'],
                        'stock' => $v['stock'],
                        'price' => $v['price'],
                        'old_price' => $v['old_price'],
                        'pin_price' => $v['pin_price'],
                        'gong_price' => $v['gong_price'],
                    );
                    $goods_attr_value_id = Db::name('goods_attr_value')
                        ->insertGetId($goods_attr_value_insert_data);
                }
            } else {
                $goods_attr_insert_data = array(
                    'goods_id' => $goods_id,
                    'attr_name' => '规格',
                    'attr_value' => '默认',
                );
                $goods_attr_value_insert_data = array(
                    'goods_id' => $goods_id,
                    'suk' => '默认',
                    'stock' => $data['stock'],
                    'price' => $data['price'],
                    'old_price' => $data['old_price'],
                    'pin_price' => $data['pin_price'],
                    'gong_price' => $data['gong_price'],
                );
                $goods_attr_id = Db::name('goods_attr')
                    ->insertGetId($goods_attr_insert_data);
                $goods_attr_value_id = Db::name('goods_attr_value')
                    ->insertGetId($goods_attr_value_insert_data);
            }
            if ($goods_id) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $goods_cate = Db::name('goods_cate')
            ->where(['is_del' => 0])
            ->order('sort desc')
            ->select();
        $brand = Db::name('brand')
            ->where(['is_del' => 0])
            ->order('sort desc')
            ->select();
        //供货商
        $supply = Db::name('admin')
            ->where([
                'status' => 1,
                'roleid' => 4
            ])
            ->order('create_time desc')
            ->select();
        $this->assign([
            'goods_cate' => $goods_cate,
            'brand' => $brand,
            'supply' => $supply,
        ]);
        return $this->fetch('add2');
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 状态
     */
    public function setstatec()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['is_show_c' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 推荐
     */
    public function recommend()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('goods')->where('id', $id)->update(['recommend' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            foreach ($ids as $id) {
                Db::name('goods')->where('id', $id)->update([
                    'is_del' => 1
                ]);
                Db::name('goods_attr')->where(['goods_id' => $id])->update([
                    'is_del' => 1
                ]);
                Db::name('goods_attr_value')->where(['goods_id' => $id])->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['is_spec'] == 0;
            $goods_insert_data = array(
                'id' => $data['id'],
                'cate_id' => $data['cate_id'],
                'brand_id' => $data['brand_id'],
                'delivery_type' => $data['delivery_type'],
                'name' => $data['name'],
                'points' => $data['points'],
                'unit' => $data['unit'],
                'hz_no' => $data['hz_no'],
                'zc_no' => $data['zc_no'],
                'shop_return' => $data['shop_return'],
                'desc' => $data['desc'],
                'img' => $data['img'],
                'sort' => $data['sort'],
                'effect' => $data['effect'],
                'quality' => $data['quality'],
                'product_address' => $data['product_address'],
                'detail' => $data['detail'],
                'guarantee' => $data['guarantee'],
                'send_address' => $data['send_address'],
                'supply_id' => $data['supply_id'],
                'create_time' => time(),
            );
            $first_img = explode(',', $data['img']);
            $goods_insert_data['first_img'] = $first_img[0];
            $goods_id = Db::name('goods')->update($goods_insert_data);

            if ($data['is_spec'] == 1) {
                if ($data['attr_name'] != $data['old_attr_name']) {
                    Db::name('goods_attr')->where('goods_id', '=', $data['id'])->delete();
                    $goods_attr_insert_data = array(
                        'goods_id' => $data['id'],
                        'attr_name' => $data['attr_name'],
                        'attr_value' => $data['attr_value'],
                    );
                    $goods_attr_id = Db::name('goods_attr')
                        ->insertGetId($goods_attr_insert_data);
                }
                if ($data['attr_value'] != $data['old_attr_value']) {
                    Db::name('goods_attr_value')->where('goods_id', '=', $data['id'])->delete();
                    foreach ($data['spec'] as $k => $v) {
                        $goods_attr_value_insert_data = array(
                            'goods_id' => $data['id'],
                            'suk' => $v['spec_name'],
                            'stock' => $v['stock'],
                            'price' => $v['price'],
                            'old_price' => $v['old_price'],
                            'pin_price' => $v['pin_price'],
                            'gong_price' => $v['gong_price'],
                        );
                        $goods_attr_value_id = Db::name('goods_attr_value')
                            ->insertGetId($goods_attr_value_insert_data);
                    }
                }
            } else {
//                $goods_attr_insert_data = array(
//                    'goods_id' => $data['id'],
//                    'attr_name' => '规格',
//                    'attr_value' => '默认',
//                );
                //                $goods_attr_id = Db::name('goods_attr')
//                    ->insertGetId($goods_attr_insert_data);
                $goods_attr_value_insert_data = array(
                    'goods_id' => $data['id'],
                    'suk' => '默认',
                    'stock' => $data['stock'],
                    'price' => $data['price'],
                    'old_price' => $data['old_price'],
                    'pin_price' => $data['pin_price'],
                    'gong_price' => $data['gong_price'],
                );

                $goods_attr_value_id = Db::name('goods_attr_value')
                    ->where([
                        'goods_id' => $data['id'],
                        'suk' => '默认'
                    ])
                    ->update($goods_attr_value_insert_data);
            }
            if ($goods_id) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $data = Db::name('goods')->find($id);
        //获取陪诊服务列表
        $goods_cate = Db::name('goods_cate')
            ->where(['is_del' => 0])
            ->order('sort desc')
            ->select();
        $brand = Db::name('brand')
            ->where(['is_del' => 0])
            ->order('sort desc')
            ->select();
        $goods_attr = Db::name('goods_attr')->where('goods_id', '=', $id)->select();
        $goods_attr2 = Db::name('goods_attr')->where('goods_id', '=', $id)->find();

        $goods_attr_value = Db::name('goods_attr_value')->where('goods_id', '=', $id)->select();
        $supply = Db::name('admin')
            ->where([
                'status' => 1,
                'roleid' => 4
            ])
            ->order('create_time desc')
            ->select();
        $this->assign([
            'goods_attr' => $goods_attr,
            'goods_attr2' => $goods_attr2,
            'goods_attr_value' => $goods_attr_value,
            'goods_cate' => $goods_cate,
            'brand' => $brand,
            'data' => $data,
            'supply' => $supply
        ]);
        $this->assign('data', $data);
        return $this->fetch('');
    }
    //设置返现
    public function fxleveledit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            Db::name('goods_level_money')->where([
                'goods_id' => $data['id']
            ])->delete();
            foreach ($data['ml'] as $k => $v) {
                $insert_data = array(
                    'goods_id' => $data['id'],
                    'level_id' => $k,
                    'rate' => $v
                );
                Db::name('goods_level_money')->insertGetId($insert_data);
            }
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $member_level = Db::name('member_level')
            ->where([
                'is_del' => 0
            ])->order('level')->select();
        foreach ($member_level as $k => $v) {
            $goods_level_money = Db::name('goods_level_money')->where([
                'level_id' => $v['id'],
                'goods_id' => $id
            ])->find();
            if ($goods_level_money) {
                $member_level[$k]['rate'] = $goods_level_money['rate'];
            } else {
                $member_level[$k]['rate'] = 0;
            }
        }
        $this->assign([
            'member_level' => $member_level,
            'id' => $id
        ]);
        return $this->fetch();
    }

    public function cvedit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'video_url' => $data['video_url'],
            );
            $stmt = Db::name('goods')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('goods')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    //todo 1拼团区 2臻品区 3砍价区 4健康区
}